package com.huan;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.Test;

import java.net.URI;



public class HDSFClient {
    public static void main(String[] args) throws Exception{
        //TODO 创建上下文对象
        Configuration entries = new Configuration();
        //TODO 获取HDFS客户端对象
        FileSystem fs = FileSystem.get( new URI( "hdfs://huan01:9000" ),entries,"root" );
        //TODO 在HDFS上创建路径
        fs.mkdirs( new Path( "/huan/dashen" ) );
        //TODO 关闭资源
        fs.close();
        System.out.println("over");

    }

    //TODO 文件上传
    @Test
    public void testCopyFromLocalFile() throws Exception {
        Configuration entries = new Configuration();
        //TODO 1,获取fs对象
        FileSystem fs = FileSystem.get( new URI( "hdfs://huan01:9000" ),entries,"root" );
        //TODO 2,执行上传API
        fs.copyFromLocalFile( new Path("E:\\Project\\Hadoop\\HDFS\\data\\huanhuan.txt") , new Path("/huan"  ));
        //TODO 3,关闭资源
        System.out.println("拿下");

    }
    //TODO 文件上传(参数优先级问题),将 hadoop下的hdfs-site.xml中的配置文件拷贝到resources中 (默认副本数是3,优先级里面设置的副本数为1)
    @Test
    public void testPut() throws Exception {
        Configuration entries = new Configuration();
        //TODO 参数 , 先是代码配置参数下的优先,中是hdfs-site.xml下的 , 而后是linux下的
        entries.set( "dfs.replication","2" );
        //TODO 1,获取fs对象
        FileSystem fs = FileSystem.get( new URI( "hdfs://huan01:9000" ),entries,"root" );
        //TODO 2,执行上传API  (先本地，后hdfs)
        fs.copyFromLocalFile( new Path("E:\\Project\\Hadoop\\HDFS\\data\\haoge.txt") , new Path("/huan"  ));
        //TODO 3,关闭资源
        System.out.println("拿下");
    }

    //TODO 将HDFS文件下载到本地,
    @Test
    public void testcopyToLocalFile() throws Exception{
        Configuration entries = new Configuration();

        FileSystem fs =  FileSystem.get( new URI( "hdfs://huan01:9000" ) ,entries,"root");
        //TODO 执行下载操作 (先hdfs , 后本地)  (第二种忽略src文件)
        //fs.copyToLocalFile( new Path( "/huan/haoge.txt" ), new Path( "E:\\Project\\Hadoop\\HDFS\\data\\dahuan.txt" ));
        fs.copyToLocalFile( false,new Path( "/huan/haoge.txt" ), new Path( "E:\\Project\\Hadoop\\HDFS\\data\\dahuan.txt" ),true );
        fs.close();
        System.out.println("嗨");
    }

    //TODO 文件删除 (文件夹和文件都可以删除)
    @Test
    public void testDeleteFile() throws Exception{
        Configuration entries = new Configuration();

        FileSystem fs = FileSystem.get( new URI( "hdfs://huan01:9000" ), entries, "root" );
        //TODO 删除
        fs.delete( new Path( "/huan/haoge.txt" ),true );
        //TODO 关闭资源
        fs.close();

        System.out.println("???");

    }

    //TODO 修改文件名称
    @Test
    public void testRename() throws Exception{
        Configuration entries = new Configuration();
        FileSystem fs = FileSystem.get( new URI( "hdfs://huan01:9000" ), entries, "root" );
        //TODO 修改文件名称
        fs.rename( new Path( "/huan/sanguo/shuguo/liubei.txt" ),new Path( "/huan/sanguo/shuguo/daige.txt" ) );
        fs.close();
        System.out.println("好像是成功了");
    }

    //TODO 查看文件详情
    @Test
    public void testList() throws Exception{
        Configuration entries = new Configuration();
        FileSystem fs = FileSystem.get( new URI( "hdfs://huan01:9000" ), entries, "root" );
        //TODO 查看文件详情
        RemoteIterator<LocatedFileStatus> files = fs.listFiles( new Path( "/huan" ), true );

        while (files.hasNext()){ //TODO 名称，权限，长度，块大小，块信息
            LocatedFileStatus file = files.next();
            System.out.println(file.getPath().getName()); //TODO 获取文件名称
            System.out.println(file.getPermission()); //TODO 获取文件权限
            System.out.println(file.getLen()); //TODO 获取文件长度
            System.out.println(file.getBlockSize()); // TODO 块大小 (默认为128mb，通常为字节类型，则  128*1024*1024 = 134217728  )

            BlockLocation[] blockLocations = file.getBlockLocations(); //TODO 拿到块的位置
            //TODO 遍历出块的信息
            for (BlockLocation blockLocation : blockLocations) {
                //TODO 拿到没个块对应的主机
                String[] hosts = blockLocation.getHosts();
                //TODO 遍历出该主机
                for (String host : hosts) {
                    System.out.println(host);
                }
            }
            System.out.println("------分割线---------");
        }
        fs.close();
    }

    //TODO 判断文件还是文件夹
    @Test
    public void testListFile() throws Exception{
        Configuration entries = new Configuration();
        FileSystem fs = FileSystem.get( new URI( "hdfs://huan01:9000" ), entries, "root" );

        //TODO 判断操作
        FileStatus[] fileStatuses = fs.listStatus( new Path( "/huan" ) );
        //TODO 先将/huan里的东西遍历出来
        for (FileStatus fileStatus : fileStatuses) {
            //TODO 进行判断操作
            if(fileStatus.isFile()){
                //TODO 默认是文件
                System.out.println("文件："+fileStatus.getPath().getName()); //TODO 先获取路径才能获取名字
            }else {
                System.out.println("文件夹："+fileStatus.getPath().getName());
            }
        }

        fs.close();
        System.out.println("好像成功了？？？？");
    }


}
